package com.ydd.app.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Repository;

import com.ydd.app.bean.common.CompanyInfoBean;
import com.ydd.app.bean.common.CompanyTypeBean;
import com.ydd.app.bean.common.ConfigTypeBean;
import com.ydd.app.bean.common.DispalyBean;
import com.ydd.app.bean.input.CreateCompanyRequest;
import com.ydd.app.bean.input.CreateConfigTypesRequest;
import com.ydd.app.bean.input.UpdateDisplayFieldRequest;
import com.ydd.app.dao.CompanyDao;
import com.ydd.common.hibernate3.JdbcBaseDao;
import com.ydd.common.util.CommonUtil;

@Repository
public class CompanyDaoImpl  extends JdbcBaseDao  implements CompanyDao{

	@Override
	public CompanyInfoBean queryCompanyInfo(final String ssId,final boolean isDispaly) {
		SqlRowSet set = null;
		if( isDispaly ) {
			set = getJdbcTemplate().queryForRowSet(CompanyDaoSql.queryInDispalyCompanyDetailSql(queryDisplay()),ssId);
		}
		else {
			set = getJdbcTemplate().queryForRowSet(CompanyDaoSql.queryCompanyDetailById(),ssId);
		}
		 
		while(set.next()){
			final CompanyInfoBean companyInfoBean = new CompanyInfoBean();
			companyInfoBean.setCompanyTitle(set.getString("CompanyTitle"));
			companyInfoBean.setAdress(set.getString("Adress"));
			companyInfoBean.setCompanyName(set.getString("CompanyName"));
			companyInfoBean.setComplete("1".equals(set.getString("Complete")));
			companyInfoBean.setCompleteDate(set.getString("CompleteDate"));
			companyInfoBean.setCompleteMoney(set.getString("CompleteMoney"));
			companyInfoBean.setCompleteUnit(set.getString("CompleteUnit"));
			companyInfoBean.setCondition(set.getString("Condition"));
			companyInfoBean.setContactTele(set.getString("ContactTele"));
			companyInfoBean.setCscale(set.getString("CScale"));
			companyInfoBean.setCtype(set.getString("Ctype"));
			companyInfoBean.setDepartment(set.getString("department"));
			companyInfoBean.setDepartmentEmail(set.getString("departmentEmail"));
			companyInfoBean.setDepartmentTele(set.getString("departmentTele"));
			companyInfoBean.setDepartmentOper(set.getString("departmentOper"));
			companyInfoBean.setExamine("1".equals(set.getString("IsExamine")));
			companyInfoBean.setExamineDate(set.getString("ExamineDate"));
			companyInfoBean.setExamineOper(set.getString("ExamineOper"));
			companyInfoBean.setIntentional(set.getString("Intentional"));
			companyInfoBean.setLegalPerson(set.getString("legalPerson"));
			companyInfoBean.setMainbusiness(set.getString("Mainbusiness"));
			companyInfoBean.setMainproducts(set.getString("Mainproducts"));
			companyInfoBean.setNature(set.getString("Nature"));
			companyInfoBean.setnDate(set.getString("nDate"));
			companyInfoBean.setOwnedindustryStr(set.getString("OwnedindustryStr"));
			companyInfoBean.setNeedsconfig(set.getString("needsconfig"));
			companyInfoBean.setNtype(set.getString("Ntype"));
			companyInfoBean.setOwnedindustry(set.getString("Ownedindustry"));
			companyInfoBean.setPolicyneeds(set.getString("Policyneeds"));
			companyInfoBean.setSendID(set.getString("SendID"));
			companyInfoBean.setSysID(set.getString("SysID"));
			companyInfoBean.setSystemDate(set.getString("SystemDate"));
			companyInfoBean.setSystemOper(set.getString("SystemOper"));
			companyInfoBean.setTotalsharecapital(set.getString("Totalsharecapital"));
			companyInfoBean.setWriteDate(set.getString("WriteDate"));
			companyInfoBean.setWriteOper(set.getString("WriteOper"));
			companyInfoBean.setWriteTele(set.getString("WriteTele"));
			companyInfoBean.setYesOrNo("1".equals(set.getString("IsYesOrNo")));
			return companyInfoBean;
		}
		return null;
	}

	@Override
	public List<CompanyInfoBean> queryCompanyInfos(final String companyTypeId,final int page, final int pageCount) {
		final String sql = CompanyDaoSql.queryCompanys(page, pageCount, companyTypeId);
	
		SqlRowSet set = getJdbcTemplate().queryForRowSet(sql);
		final List<CompanyInfoBean> companys = new ArrayList<CompanyInfoBean>();
		while(set.next()){
			final CompanyInfoBean companyInfoBean = new CompanyInfoBean();
			companyInfoBean.setCompanyTitle(set.getString("CompanyTitle"));
//			companyInfoBean.setAdress(set.getString("Adress"));
//			companyInfoBean.setCompanyName(set.getString("CompanyName"));
//			companyInfoBean.setComplete("1".equals(set.getString("Complete")));
			companyInfoBean.setCompleteDate(set.getString("CompleteDate"));
//			companyInfoBean.setCompleteMoney(set.getString("CompleteMoney"));
//			companyInfoBean.setCompleteUnit(set.getString("CompleteUnit"));
//			companyInfoBean.setCondition(set.getString("Condition"));
//			companyInfoBean.setContactTele(set.getString("ContactTele"));
//			companyInfoBean.setCscale(set.getString("CScale"));
//			companyInfoBean.setCtype(set.getString("Ctype"));
//			companyInfoBean.setDepartment(set.getString("department"));
//			companyInfoBean.setDepartmentEmail(set.getString("departmentEmail"));
//			companyInfoBean.setDepartmentTele(set.getString("departmentTele"));
//			companyInfoBean.setDepartmentOper(set.getString("departmentOper"));
//			companyInfoBean.setExamine("1".equals(set.getString("IsExamine")));
			companyInfoBean.setExamineDate(set.getString("ExamineDate"));
//			companyInfoBean.setExamineOper(set.getString("ExamineOper"));
//			companyInfoBean.setIntentional(set.getString("Intentional"));
//			companyInfoBean.setLegalPerson(set.getString("legalPerson"));
//			companyInfoBean.setMainbusiness(set.getString("Mainbusiness"));
//			companyInfoBean.setMainproducts(set.getString("Mainproducts"));
//			companyInfoBean.setNature(set.getString("Nature"));
//			companyInfoBean.setnDate(set.getString("nDate"));
			companyInfoBean.setOwnedindustryStr(set.getString("OwnedindustryStr"));
//			companyInfoBean.setNeedsconfig(set.getString("needsconfig"));
//			companyInfoBean.setNtype(set.getString("Ntype"));
			companyInfoBean.setOwnedindustry(set.getString("Ownedindustry"));
			companyInfoBean.setPolicyneeds(set.getString("Policyneeds"));
//			companyInfoBean.setSendID(set.getString("SendID"));
			companyInfoBean.setSysID(set.getString("SysID"));
			companyInfoBean.setSystemDate(set.getString("SystemDate"));
//			companyInfoBean.setSystemOper(set.getString("SystemOper"));
//			companyInfoBean.setTotalsharecapital(set.getString("Totalsharecapital"));
			companyInfoBean.setWriteDate(set.getString("WriteDate"));
//			companyInfoBean.setWriteOper(set.getString("WriteOper"));
//			companyInfoBean.setWriteTele(set.getString("WriteTele"));
//			companyInfoBean.setYesOrNo("1".equals(set.getString("IsYesOrNo")));
			companys.add(companyInfoBean);
		} 
		if( null != companys && companys.size() > 0 ) {
			return companys;
		}
		else {
			return null;
		}
	}

	@Override
	public List<ConfigTypeBean> queryNewestCompanyInfos( final int pageCount) {
		final List<ConfigTypeBean> types = queryConfigTypes();
		for (ConfigTypeBean  companyType: types) {
			if( null != companyType ) {
				final List<CompanyInfoBean> companies = queryNewestCompanyInfoByTypeId(companyType.getSysId(), pageCount);
				if( null != companies ) {
					companyType.setCompanies(companies.toArray(new CompanyInfoBean[0]));
				}
			}
		}
		return types;
	}

	@Override
	public List<ConfigTypeBean> queryConfigTypes() {
		SqlRowSet set = getJdbcTemplate().queryForRowSet(CompanyDaoSql.QUERY_CONFIG_SQL);
		final List<ConfigTypeBean> types = new ArrayList<ConfigTypeBean>();
		while(set.next()){
			final ConfigTypeBean type = new ConfigTypeBean();
			type.setName(set.getString("Names"));
			type.setSysId(set.getString("SysID"));
			type.setSendID(set.getString("SendID"));
			types.add(type);
		}
		if( null != types && types.size() > 0 ) {
			return types;
		}
		else {
			return null;
		}
	}

	@Override
	public int queryCompanyCount(final String companyTypeId) {
		if( CommonUtil.isNotEmpty(companyTypeId)) {
			final String sql = " select * from TBL_CompanyInfo where DeleteRecord = '0' " +
					"and ( select count(*) from dbo.func_splitid(Ownedindustry,',') where value = "+companyTypeId+" ) > 0 " ;
			return getRowCountHql(sql);
		}
		else {
			final String sql = " select * from TBL_CompanyInfo where DeleteRecord = '0' " ;
			return getRowCountHql(sql);
		}
	}
	
	public List<CompanyInfoBean> queryNewestCompanyInfoByTypeId(final String typeId, final int pageCount) {
		final String sql = CompanyDaoSql.queryCompanys(0, pageCount,typeId);
		SqlRowSet set = getJdbcTemplate().queryForRowSet(sql);
		final List<CompanyInfoBean> companys = new ArrayList<CompanyInfoBean>();
		while(set.next()){
			final CompanyInfoBean companyInfoBean = new CompanyInfoBean();
			companyInfoBean.setCompanyTitle(set.getString("CompanyTitle"));
//			companyInfoBean.setAdress(set.getString("Adress"));
//			companyInfoBean.setCompanyName(set.getString("CompanyName"));
//			companyInfoBean.setComplete("1".equals(set.getString("Complete")));
//			companyInfoBean.setCompleteDate(set.getString("CompleteDate"));
//			companyInfoBean.setCompleteMoney(set.getString("CompleteMoney"));
//			companyInfoBean.setCompleteUnit(set.getString("CompleteUnit"));
//			companyInfoBean.setCondition(set.getString("Condition"));
//			companyInfoBean.setContactTele(set.getString("ContactTele"));
//			companyInfoBean.setCscale(set.getString("CScale"));
//			companyInfoBean.setCtype(set.getString("Ctype"));
//			companyInfoBean.setDepartment(set.getString("department"));
//			companyInfoBean.setDepartmentEmail(set.getString("departmentEmail"));
//			companyInfoBean.setDepartmentTele(set.getString("departmentTele"));
//			companyInfoBean.setDepartmentOper(set.getString("departmentOper"));
//			companyInfoBean.setExamine("1".equals(set.getString("IsExamine")));
//			companyInfoBean.setExamineDate(set.getString("ExamineDate"));
//			companyInfoBean.setExamineOper(set.getString("ExamineOper"));
//			companyInfoBean.setIntentional(set.getString("Intentional"));
//			companyInfoBean.setLegalPerson(set.getString("legalPerson"));
//			companyInfoBean.setMainbusiness(set.getString("Mainbusiness"));
//			companyInfoBean.setMainproducts(set.getString("Mainproducts"));
//			companyInfoBean.setNature(set.getString("Nature"));
//			companyInfoBean.setnDate(set.getString("nDate"));
			companyInfoBean.setOwnedindustryStr(set.getString("OwnedindustryStr"));
//			companyInfoBean.setNeedsconfig(set.getString("needsconfig"));
//			companyInfoBean.setNtype(set.getString("Ntype"));
			companyInfoBean.setOwnedindustry(set.getString("Ownedindustry"));
//			companyInfoBean.setPolicyneeds(set.getString("Policyneeds"));
//			companyInfoBean.setSendID(set.getString("SendID"));
			companyInfoBean.setSysID(set.getString("SysID"));
//			companyInfoBean.setSystemDate(set.getString("SystemDate"));
//			companyInfoBean.setSystemOper(set.getString("SystemOper"));
//			companyInfoBean.setTotalsharecapital(set.getString("Totalsharecapital"));
//			companyInfoBean.setWriteDate(set.getString("WriteDate"));
//			companyInfoBean.setWriteOper(set.getString("WriteOper"));
//			companyInfoBean.setWriteTele(set.getString("WriteTele"));
//			companyInfoBean.setYesOrNo("1".equals(set.getString("IsYesOrNo")));
			companys.add(companyInfoBean);
		} 
		if( null != companys && companys.size() > 0 ) {
			return companys;
		}
		else {
			return null;
		}
	}

	@Override
	public List<CompanyTypeBean> queryCompanyTypes() {
		SqlRowSet set = getJdbcTemplate().queryForRowSet(CompanyDaoSql.QUERY_COMPANY_TYPES_SQL);
		final List<CompanyTypeBean> companies = new ArrayList<CompanyTypeBean>();
		while(set.next()){
			final CompanyTypeBean company = new CompanyTypeBean();
			company.setCompanyTypeId(set.getString("company_type_id"));
			company.setCompanyTypeName(set.getString("company_type_name"));
			companies.add(company);
		}
		if( null != companies && companies.size() > 0 ) {
			return companies;
		}
		else {
			return null;
		}
	}

	@Override
	public boolean queryConfigTypeIsExist(String typeId) {
		final String sql = " select count(SysID) as count from  TBL_ConfigType where 	SysID = ? ";
		final List<Integer> counts = getJdbcTemplate().queryForList(sql, Integer.class,typeId ) ;
		if( null != counts && counts.size() > 0 ) {
			if( counts.get(0) > 0 ) {
				return true;
			} 
		}
		return false;
	}

	public List<DispalyBean> queryDisplay() {
		SqlRowSet set = getJdbcTemplate().queryForRowSet(CompanyDaoSql.queryDispalySql());
		final List<DispalyBean> types = new ArrayList<DispalyBean>();
		while(set.next()){
			final DispalyBean type = new DispalyBean();
			type.setDisplay(set.getString("Display"));
			type.setDisplayName(set.getString("DisplayName"));
			type.setFieldName(set.getString("FieldName"));
			types.add(type);
		}
		if( null != types && types.size() > 0 ) {
			return types;
		}
		else {
			return null;
		}
	}
	
	public boolean createCompany(final CreateCompanyRequest request) {
		if( getJdbcTemplate().update(CompanyDaoSql.UPDATE_COMPANY_SQL,
				request.getCompanyName(),
				request.getnDate(),
				request.getLegalPerson(),
				request.getTotalsharecapital(),
				request.getAdress(),
				request.getContactTele(),
				request.getCtype(),
				request.getNature(),
				request.getCScale(),
				request.getMainbusiness(),
				request.getMainproducts(),
				request.getOwnedindustry(),
				request.getOwnedindustryStr(),
				request.getNtype(),
				request.getCondition(),
				request.getIntentional(),
				request.getPolicyneeds(),
				request.getNeedsconfig(),
				request.getDepartment(),
				request.getDepartmentOper(),
				request.getDepartmentTele(),
				request.getDepartmentEmail(),
				request.getWriteOper(),
				request.getWriteTele(),
				request.getWriteDate(),
				request.getIsExamine(),
				request.getIsYesOrNo(),
				request.getExamineOper(),
				request.getExamineDate(),
				request.getDeleteRecord(),
				request.getSendID(),
				request.getComplete(),
				request.getCompleteUnit(),
				request.getCompleteMoney(),
				request.getCompleteDate(),
				request.getSystemOper(),
				request.getSystemDate(),
				request.getCompanyTitle(),
				request.getSysID()) > 0) {
			return true;
		}
		else {
			return getJdbcTemplate().update(CompanyDaoSql.INSERT_COMPANY_SQL,
					request.getSysID(),
					request.getCompanyName(),
					request.getnDate(),
					request.getLegalPerson(),
					request.getTotalsharecapital(),
					request.getAdress(),
					request.getContactTele(),
					request.getCtype(),
					request.getNature(),
					request.getCScale(),
					request.getMainbusiness(),
					request.getMainproducts(),
					request.getOwnedindustry(),
					request.getOwnedindustryStr(),
					request.getNtype(),
					request.getCondition(),
					request.getIntentional(),
					request.getPolicyneeds(),
					request.getNeedsconfig(),
					request.getDepartment(),
					request.getDepartmentOper(),
					request.getDepartmentTele(),
					request.getDepartmentEmail(),
					request.getWriteOper(),
					request.getWriteTele(),
					request.getWriteDate(),
					request.getIsExamine(),
					request.getIsYesOrNo(),
					request.getExamineOper(),
					request.getExamineDate(),
					request.getDeleteRecord(),
					request.getSendID(),
					request.getComplete(),
					request.getCompleteUnit(),
					request.getCompleteMoney(),
					request.getCompleteDate(),
					request.getSystemOper(),
					request.getSystemDate(),
					request.getCompanyTitle()) > 0 ;
		}
	}
	
	public boolean createCOnfigType(final CreateConfigTypesRequest request){
		if(getJdbcTemplate().update(CompanyDaoSql.UPDATE_CONFIG_TYPE_SQL,
				request.getNames(),
				request.getSendID(),
				request.getSysID()) > 0 ) {
			return true;
		}
		else {
			return getJdbcTemplate().update(CompanyDaoSql.INSERT_CONFIG_TYPE_SQL,
					request.getSysID(),
					request.getNames(),
					request.getSendID()) > 0 ;
		}
	}
	
	public boolean updateDisplayField(final UpdateDisplayFieldRequest request){
		if(getJdbcTemplate().update(CompanyDaoSql.UPDATE_DISPALY_TYPE_SQL,
				request.getDisplayName(),
				request.getDisplay(),
				request.getVisible(),
				request.getFieldName()) > 0 ) {
			return true;
		}
		else {
			return getJdbcTemplate().update(CompanyDaoSql.INSERT_DISPALY_TYPE_SQL,
					request.getDisplayName(),
					request.getDisplay(),
					request.getVisible(),request.getFieldName()) > 0;
		}
	}
}
